package com.fsh.subject.domain.handler.subject;

import com.fsh.subject.common.enums.IsDeletedFlagEnum;
import com.fsh.subject.common.enums.SubjectInfoTypeEnum;
import com.fsh.subject.domain.convert.MultipleSubjectConverter;
import com.fsh.subject.domain.entity.SubjectAnswerBO;
import com.fsh.subject.domain.entity.SubjectInfoBO;
import com.fsh.subject.domain.entity.SubjectOptionBO;
import com.fsh.subject.infra.basic.entity.SubjectMultiple;
import com.fsh.subject.infra.basic.service.SubjectMultipleService;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;


/**
 * 多选题目的策略类
 *
 * @author: ChickenWing
 * @date: 2023/10/5
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectMultipleService subjectMultipleService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        Preconditions.checkNotNull(subjectInfoBO.getOptionList(),"选项不能为空");
        Preconditions.checkArgument(!subjectInfoBO.getOptionList().isEmpty(),"没有对应的选项");
        List<SubjectMultiple> subjectMultipleList=new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(option->{
            SubjectMultiple subjectMultiple = MultipleSubjectConverter.INSTANCE.convertBOToMultiple(option);
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            subjectMultipleList.add(subjectMultiple);
        });
        subjectMultipleService.batchInsert(subjectMultipleList);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectMultiple subjectMultiple=new SubjectMultiple();
        subjectMultiple.setSubjectId(subjectId);
        subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        //查询 多选题表
        List<SubjectMultiple> subjectMultipleList = subjectMultipleService.queryByCondition(subjectMultiple);
        //转换
        List<SubjectAnswerBO> subjectAnswerBOS = MultipleSubjectConverter.INSTANCE.convertMultipleToAnswerBOList(subjectMultipleList);

        //构造SubjectOptionBO对象
        SubjectOptionBO subjectOptionBO=new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOS);

        return subjectOptionBO;
    }

}
